package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.controller.ZnxController;
import com.example.entity.hz.Znx;
import com.example.entity.yifei.Invtj;
import com.example.entity.yifei.Invtk;
import com.example.exception.BaseException;
import com.example.exception.CustomException;
import com.example.mapper.InvtkMapper;
import com.example.service.IActFlowCustomService;
import com.example.service.InvtjService;
import com.example.mapper.InvtjMapper;
import com.example.untils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 14501
* @description 针对表【INVTJ】的数据库操作Service实现
* @createDate 2024-08-15 13:18:35
*/
@Service
public class InvtjServiceImpl extends ServiceImpl<InvtjMapper, Invtj> implements InvtjService, IActFlowCustomService {

    @Resource
    private InvtjMapper invtjMapper;

    @Resource
    private InvtkMapper invtkMapper;

    @Override
    public Boolean tc30IsN(String tc001, String tc002) {
        LambdaQueryWrapper<Invtj> queryWrapper= new LambdaQueryWrapper<Invtj>();
        queryWrapper.eq(Invtj::getTJ001,tc001).eq(Invtj::getTJ002,tc002);
        Invtj invtj = invtjMapper.selectOne(queryWrapper);
        if (invtj!=null&&invtj.getTJ015().equals("0")){
            return true;
        }else {
            throw new BaseException(tc001+"-"+tc002+"该存货调整单不是未审核存货调整单");
        }
    }

    @Override
    public Map<String, Object> setvariables(String id, String userName) {
        Map variables=new HashMap<>();
        variables.put("assignee0",userName);
        variables.put("processInsName","核价单流程");
        return variables;
    }

    @Override
    public Map<String, Object> setvariables(String id, String userName, Object vo) {
        return null;
    }

    @Autowired
    private ZnxController znxController;
    @Override
    public void startRunTask(String id) {

    }

    @Override
    public void endRunTask(String id, Boolean state, String defineId, String initiator) {
        System.out.printf("存货调整单审批流程结束业务操作！！！！id="+id+"审核状态为："+state);
        String[] strings = id.split(":");
        Invtj invtj = invtjMapper.selectOne(new LambdaQueryWrapper<Invtj>().eq(Invtj::getTJ001, strings[0]).eq(Invtj::getTJ002, strings[1]));
        if(invtj == null) {
            throw new CustomException("500", "存货调整单不存在");
        }
        if(state) {  // 审核通过
            invtj.setTJ015("3");
            invtjMapper.update(invtj, new LambdaQueryWrapper<Invtj>().eq(Invtj::getTJ001, strings[0]).eq(Invtj::getTJ002, strings[1]));
            checkcopta(strings[0], strings[1]);
            List<Znx> znxList=new ArrayList<>();
            Znx znx=Znx.builder().code(id).content("有一张新的存货调整单通过了审核!").departmentCode("90802").notificationCode(initiator).customField(defineId)
                    .title("通知").build();
            znxList.add(znx);
            znxController.insert(znxList);
        }else{  // 审批失败
            invtj.setTJ015("2");
            invtjMapper.update(invtj, new LambdaQueryWrapper<Invtj>().eq(Invtj::getTJ001, strings[0]).eq(Invtj::getTJ002, strings[1]));
        }
    }

    public void checkcopta(String TA001, String TA002) {
        if(StringUtils.isEmpty(TA001) || StringUtils.isEmpty(TA002)) {
            throw new CustomException("500", "单别或单号为空");
        }
        Invtj invtj = invtjMapper.selectOne(new LambdaQueryWrapper<Invtj>().eq(Invtj::getTJ001, TA001).eq(Invtj::getTJ002, TA002));
        if(invtj == null) {
            throw new CustomException("500", "单据不存在!");
        }
        invtj.setTJ010("Y");
        invtjMapper.update(invtj, new LambdaQueryWrapper<Invtj>().eq(Invtj::getTJ001, TA001).eq(Invtj::getTJ002, TA002));
        Invtk invtk = new Invtk();
        invtk.setTK023("Y");
        invtkMapper.update(invtk, new LambdaQueryWrapper<Invtk>().eq(Invtk::getTK001, TA001).eq(Invtk::getTK002, TA002));
    }

    @Override
    public Object taskInfo(Map<String, Object> processVariables) {
        return processVariables.get("object");
    }

}




